/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.group4.highschoolportal.forget;

import com.group4.controller.StudentFacade;
import com.group4.entity.Student;
import com.group4.highschoolportal.service.MailerBean;
import com.group4.highschoolportal.service.RestSmsClient;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Andreas
 */
@ManagedBean
@RequestScoped
public class ForgotBean {

    @EJB
    private StudentFacade studentDb;
    @ManagedProperty(value = "#{mailer}")
    private MailerBean mailer;
    @ManagedProperty(value = "#{sms}")
    private RestSmsClient sms;
    private String type;
    private String username;

    /** Creates a new instance of ForgotBean */
    public ForgotBean() {
    }

    /*
     * Listen after someone posts and then
     * sends their password to either sms or phone
     * and returns a faces-config action
     */
    public String postListener() {

        Student stud = studentDb.findPassword(username);
        
        //user exists
        if (stud != null) {
            String pass = stud.getUser().getPassword();
            
            if ("email".equals(type)) {
                mailer.sendMail(stud.getUser().getEmail(), "Your High School Portal password",
                        "Your password is: " + pass);
            } else {
                sms.sendSMS("+46735888940", "Your password is: " + pass); // hardcoded number to test!
            }
            return "passwordSent";
        }
        return "noUserFound";
    }

    //SETTERS AND GETTERS
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public MailerBean getMailer() {
        return mailer;
    }

    public void setMailer(MailerBean mailer) {
        this.mailer = mailer;
    }

    public RestSmsClient getSms() {
        return sms;
    }

    public void setSms(RestSmsClient sms) {
        this.sms = sms;
    }
}
